package com.example.administrator.kotlin.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.fastjson.JSONObject
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.adapter.MyAdapter
import com.example.administrator.kotlin.bean.NewsBean
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import kotlinx.android.synthetic.main.item_fragment.*

/**
 * Created by Administrator on 2018/2/27/027.
 */
class FirstPageFragment : Fragment() {

    var TAG: String = FirstPageFragment.javaClass.simpleName

    companion object {
        fun newInstance(): FirstPageFragment {
            return FirstPageFragment()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val inflate = inflater?.inflate(R.layout.item_fragment, container, false)
        initData()
//        initView(inflate)
        return inflate
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        recyclerview.layoutManager = LinearLayoutManager(activity)
        val adapter = MyAdapter(initData(), activity)
        recyclerview.adapter = adapter
    }
    private fun initData(): ArrayList<String> {
        val arrayList = ArrayList<String>()
        for (i in 1..10) {
            arrayList.add("item :" + i)
        }
        Log.i("tag", arrayList.size.toString())
        return arrayList
    }

    private fun initView(inflate: View?) {
        var url: String = "http://api.tianapi.com/social/?key=861a794733dd2b057e415d822bef9586&num=3"
//        val recyclerView = inflate?.findViewById(R.id.recyclerview) as RecyclerView
//        recyclerView.layoutManager = LinearLayoutManager(activity)
//        val adapter = MyAdapter(initData(), activity)
//        recyclerView.adapter = adapter
//        getNews(url)
    }

    //获取新闻列表
    private fun getNews(url: String) {
        OkGo.get<String>(url)
                .tag(this)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        println(response?.body().toString())
                        val list = JSONObject.parseObject(response?.body(), NewsBean::class.java);
                    }

                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {

                    }
                })

    }

}